package de.tum.in.eist.poll.client.control;

import java.util.ArrayList;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.Widget;

import de.tum.in.eist.poll.client.gui.Dashboard;
import de.tum.in.eist.poll.client.gui.LectureCreatePanel;
import de.tum.in.eist.poll.shared.Lecture;
import de.tum.in.eist.poll.shared.User;

public class LectureCreateHandler {

	private LectureCreatePanel create;
	private AsyncCallback<Lecture> callbackOnce;
	private AsyncCallback<ArrayList<Lecture>> callbackWeekly;
	private User user;
	private ClickHandler handler;
	private Dashboard dashboard;
	
	public LectureCreateHandler(Dashboard dashboard, User user, LectureCreatePanel createLecture) {
		this.dashboard = dashboard;
		this.create = createLecture;
		this.user = user;
		init();
	}

	private void init() {
		initCallback();
		handler = createClickHandler();
		create.addHideButtonClickHandler(handler);
		create.getCreateButton().addClickHandler(handler);
		create.getOnceCheckBox().addClickHandler(handler);
		create.getWeeklyCheckBox().addClickHandler(handler);
	}

	/**
	 * initialize the {@link AsyncCallback} to react on faulty or successful
	 * tries to create a new lecture.
	 */
	public void initCallback() {
		callbackOnce = new AsyncCallback<Lecture>() {

			@Override
			public void onFailure(Throwable caught) {
				create.getErrorLabel().setText(caught.getMessage());
			}

			/**
			 * send the new lecture from the server to the view 
			 * @param result the new lecture
			 */
			@Override
			public void onSuccess(Lecture result) {
				create.getErrorLabel().setText("");
				minimizeMaximizeCreate();
				dashboard.getWidgetList().update();
			}
		};
		
		callbackWeekly = new AsyncCallback<ArrayList<Lecture>>() {

			@Override
			public void onFailure(Throwable caught) {
				create.getErrorLabel().setText(caught.getMessage());
			}

			/**
			 * send the new lectures from the server to the view 
			 * @param result the new lectures
			 */
			@Override
			public void onSuccess(ArrayList<Lecture> result) {
				create.getErrorLabel().setText("");
				minimizeMaximizeCreate();
				dashboard.getWidgetList().update();
			}
		};
	}
	
	private void minimizeMaximizeCreate() {
		if (create.isMinimized()) {
			create.expand();
		} else {
			create.collapse();
		}
	}

	/**
	 * @return a ClickHandler to create a new lecture
	 */
	private ClickHandler createClickHandler() {
		return new ClickHandler() {
			/**
			 * Create a new lecture and send it to the server
			 */
			@Override
			public void onClick(ClickEvent event) {
				Object obj = event.getSource();
				//Hide button - close create lecture view
				if (obj.equals(create.getHideButton())) {
					if (create.isMinimized()) {
						((Widget)event.getSource()).addStyleName("collapse");
					} else {
						((Widget)event.getSource()).removeStyleName("collapse");
					}
					minimizeMaximizeCreate();
				} 
				// Checkbox once
				else if (obj.equals(create.getOnceCheckBox())) {
					create.getWeeklyCheckBox().setValue(false);
					create.getOnceCheckBox().setValue(true);
					create.removeWeeklyPanel();
					create.getErrorLabel().setText("");
				} 
				// Checkbox weekly
				else  if (obj.equals(create.getWeeklyCheckBox())) {
					create.getWeeklyCheckBox().setValue(true);
					create.getOnceCheckBox().setValue(false);
					create.addWeeklyPanel();
				}
				// create Button
				else if (obj.equals(create.getCreateButton())) {
					// Add a lecture once
					if (create.getOnceCheckBox().getValue()) {
						if (create.getTopic().length() > 0
								&& create.getDateFrom() != null) {
							EIST_POLL.pollSvc.addNewLecture(user,
									create.getTopic(), create.getDateFrom(),
									callbackOnce);
						} 
						
						// Error notifications						
						else {
							boolean first = true;
							String error = "Enter ";
							if (create.getTopic().length() <= 0) {
								error += "a topic";
								first = false;
							}
							if (create.getDateFrom() == null) {
								if (!first)
									error += " and a date from";
								else
									error += "a date from";
							}
							create.getErrorLabel().setText(error + "!");
						}
					}
					// Add a lecture weekly
					else if (create.getWeeklyCheckBox().getValue()) {
						if (create.getTopic().length() > 0
								&& create.getDateFrom() != null
								&& create.getDateTo() != null) {
							// Check if date from and date to is possible / not negative
							// if not, set an error notification
							if (create.getDateFrom().compareTo(create.getDateTo()) > 0) {
								create.getErrorLabel().setText("The date to should be" +
										" at least the same day as the date from!");
							} 
							// Correct input - create the lecture/s
							else {
								// TODO edit addNewLecture() to support weekly lectures
								EIST_POLL.pollSvc.addMultipleNewLectures(user,
										create.getTopic(), create.getDateFrom(),
										create.getDateTo(), callbackWeekly);
							}
						} else {
							// Error notifications
							boolean first = true;
							String error = "Enter ";
							if (create.getTopic().length() <= 0) {
								error += "a topic";
								first = false;
							}
							if (create.getDateFrom() == null) {
								if (!first)
									error += ", a date from";
								else
									error += "a date from";
								first = false;
							}
							if (create.getDateTo() == null) {
								if (!first)
									error += " and a date to";
								else
									error += "a date to";
							}
							create.getErrorLabel().setText(error + "!");
						}
					}
				}
			}
		};
	}
}
